package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdPageRequest;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.dao.UserEquityPropsDao;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.dto.HoldCollectionDTO;
import com.sc.nft.entity.dto.HoldPropDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.EquityPropsTypeEnum;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.UserCollectionService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/collectibleStatistics")
@Api(tags = "藏品统计模块")
public class CollectibleStatisticsController {

    private final UserCollectionService userCollectionService;

    private final DigitalCollectionDao digitalCollectionDao;

    private final StaticDataDao staticDataDao;

    private final UserEquityPropsDao userEquityPropsDao;

    @ApiOperation(value = "持仓统计", notes = "持仓统计", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/positionStatistical", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<PositionStatisticalVO> positionStatistical() {
        StaticData staticData = staticDataDao.getByType("share_url");
        PositionStatisticalVO positionStatisticalVO = digitalCollectionDao.positionStatistical();
        //持仓道具数量
        positionStatisticalVO.setPropsCount(userEquityPropsDao.getHavingEquityPropsCountByUserId(EquityPropsTypeEnum.VIRTUAL_PROPS, UserPool.getUser().getId()));
        positionStatisticalVO.setShareUrl(StrUtil.format("{}{}", staticData.getValue(), UserPool.getUser().getInviteCode()));
        return Result.success(positionStatisticalVO);
    }

    @ApiOperation(value = "持有藏品", notes = "持有藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/holdCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<HoldCollectionVO>> holdCollection(@RequestBody HoldCollectionDTO holdCollectionDTO) {
        return Result.success(userCollectionService.holdCollection(holdCollectionDTO.getCollectionName()));
    }

    @ApiOperation(value = "单个持有藏品详情", notes = "单个持有藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/singleHoldCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SingleHoldCollection> singleHoldCollection(@RequestBody IdRequest idRequest) {
        return PageResult.success(userCollectionService.singleHoldCollection(idRequest.getId()));
    }

    @ApiOperation(value = "分页藏品持仓详情", notes = "单个持有藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/singleHoldCollectionDetailsPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SingleHoldCollectionDetails> singleHoldCollectionDetailsPage(@RequestBody IdPageRequest idPageRequest) {
        return PageResult.success(userCollectionService.singleHoldCollectionDetailsPage(idPageRequest.getPageNo(),idPageRequest.getPageSize(),idPageRequest.getId()));
    }

    @ApiOperation(value = "持有道具", notes = "持有道具", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/holdProp", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<HoldPropVO>> holdProp(@RequestBody HoldPropDTO holdPropDTO) {
        return Result.success(userCollectionService.holdProp(holdPropDTO.getPropName()));
    }

    @ApiOperation(value = "单个持有道具详情", notes = "单个持有道具详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/singleHoldEquityProp", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SingleHoldEquityProp> singleHoldEquityProp(@RequestBody IdRequest idRequest) {
        return PageResult.success(userCollectionService.singleHoldEquityProp(idRequest.getId()));
    }

    @ApiOperation(value = "分页道具持仓详情", notes = "单个持有道具详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/singleHoldEquityPropDetailsPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SingleHoldEquityPropDetails> singleHoldEquityPropDetailsPage(@RequestBody IdPageRequest idPageRequest) {
        return PageResult.success(userCollectionService.singleHoldEquityPropDetailsPage(idPageRequest.getPageNo(),idPageRequest.getPageSize(),idPageRequest.getId()));
    }

}
